package com.example.springcloudwinter.mapper;

import com.example.springcloudwinter.bo.BusinessBo;
import com.example.springcloudwinter.bo.CartBo;
import com.example.springcloudwinter.bo.FoodBo;
import com.example.springcloudwinter.bo.OrderBo;
import com.example.springcloudwinter.po.OrderPo;
import org.apache.ibatis.annotations.*;

import java.sql.SQLException;
import java.util.List;

@Mapper
public interface PaymentMapper {
    @Select("SELECT orderId,userId,businessId,orderDate,orderTotal,daId,orderState FROM orders WHERE userId=#{userId}")
    List<OrderPo> getOrdersByUserId(@Param("userId") String userId)throws SQLException;

    @Select("SELECT orderId,userId,businessId,orderDate,orderTotal,daId,orderState FROM orders WHERE userId=#{userId}" +
            " AND orderId=#{orderId}")
    OrderPo getOrderById(@Param("orderId")int orderId, @Param("userId")String userId)throws SQLException;

    @Select("SELECT orderId,userId,businessId,orderDate,orderTotal,daId,orderState FROM orders WHERE userId=#{userId}" +
            " AND orderId=#{orderId} AND orderState=0")
    OrderPo getNonPayOrderById(@Param("orderId")int orderId,@Param("userId")String userId)throws SQLException;

    @Select("SElECT foodId,businessId,foodPrice,foodName,foodExplain,foodImg,remarks,quantity FROM orders NATURAL JOIN orderdetailet " +
            "NATURAL JOIN food WHERE userId=#{userId} AND orderId=#{orderId}")
    List<FoodBo> getFoodById(@Param("orderId")int orderId, @Param("userId")String userId)throws SQLException;

    //不提取图片
    @Select("SElECT foodId,businessId,foodPrice,foodName,foodExplain,remarks,quantity FROM orders NATURAL JOIN orderdetailet " +
            "NATURAL JOIN food WHERE userId=#{userId} AND orderId=#{orderId}")
    List<FoodBo> getFoodById8Lightweight(@Param("orderId")int orderId,@Param("userId")String userId)throws SQLException;

    @Select("SELECT businessId,businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice," +
            "deliveryPrice,remarks FROM business NATURAL JOIN orders WHERE #{orderId}=orderId AND userId=#{userId}")
    BusinessBo getByRestaurantId(@Param("orderId")int orderId, @Param("userId")String userId)throws SQLException;

    //不提取图片
    @Select("SELECT businessId,businessName,businessAddress,businessExplain,orderTypeId,starPrice," +
            "deliveryPrice,remarks FROM business NATURAL JOIN orders WHERE #{orderId}=orderId AND userId=#{userId}")
    BusinessBo getByRestaurantId8Lightweight(@Param("orderId")int orderId,@Param("userId")String userId)throws SQLException;

    @Insert("INSERT INTO orders (userId,businessId,orderDate,orderTotal,daId,orderState) VALUES" +
            "(#{userId},#{businessId},now(),#{orderTotal},#{daId},#{orderState})")
    @Options(useGeneratedKeys = true, keyProperty = "orderId")//将自增主键返回
    void createOrder(OrderBo order)throws SQLException;

    @Update("UPDATE orders SET orderState=1, orderDate=now() WHERE orderId=#{orderId} AND userId=#{userId}")
    int payOrder (@Param("orderId")int id,@Param("userId")String userId)throws SQLException;

    @Delete("DELETE FROM cart WHERE userId=#{userId} AND businessId=#{businessId}")
    void removeCart(@Param("userId") String userId,@Param("businessId") int businessId)throws SQLException;

    @Insert("INSERT INTO orderdetailet (orderId,foodId,quantity) VALUES (#{orderId},#{foodId},#{quantity})")
    void insertOrderDetailet(@Param("orderId")int orderId,@Param("foodId")int foodId,@Param("quantity")int quantity);

    @Select("SELECT * FROM orders " +
            "WHERE userId=#{userId} AND orderState=1 AND orderDate < now() AND DATEDIFF(now(), orderDate) <= 5 " +
            "ORDER BY orderDate")
    List<OrderPo> getValidPoints(@Param("userId")String userId) throws SQLException;

    @Update("update orders set remainingPoints=#{points} where orderId=#{orderId}")
    void updatePoints(@Param("orderId")int orderId, @Param("points")int points) throws SQLException;

    @Select("SELECT cartId,foodId,businessId,userId,quantity FROM cart WHERE userId=#{userId} AND businessId=#{businessId}")
    List<CartBo> listCart(@Param("businessId")int businessId, @Param("userId")String userId)throws SQLException;
}
